package com.ccb.lms.action;

import com.ccb.lms.facade.FacadeService;
import com.ccb.lms.model.LibraryAdmin;
import com.ccb.lms.model.Reader;
import com.ccb.lms.model.SystemAdmin;


public class LoginAction extends AbstractAction{
	
	/**
	 * @author zhouxin and wangjunjie
	 */
	
	private static final long serialVersionUID = 1L;
	
	private FacadeService facadeService;
	private Reader reader;
	private LibraryAdmin libraryAdmin;
	private SystemAdmin systemAdmin;
	

	private String  username;
	private String  password;
	private String loginRole;
	

	@SuppressWarnings("unchecked")
	@Override
	public String execute() throws Exception {
		if(loginRole.equalsIgnoreCase("reader") ){
			reader=new Reader();
			reader.setReaderID(username);
			reader.setReaderPassword(password);
			if(facadeService.getReaderService().isLogin(reader)){
				//ActionContext.getContext().put("reader", reader);
				reader=facadeService.getReaderService().getReaderByID(username);
				this.session.put("reader", reader);
				return "READER_SUCCESS";
			}else{
				actionErrorMessage="用户名或密码错误";
				return "INPUT";
			}

		}else if(loginRole.equalsIgnoreCase("libraryAdmin")){
			libraryAdmin=new LibraryAdmin();
			libraryAdmin.setLibraryAdminID(username);
			libraryAdmin.setLibraryAdminPassword(password);
			if(facadeService.getLibraryAdminService().isLogin(libraryAdmin)){
				libraryAdmin=facadeService.getLibraryAdminService().getLibraryAdminByID(username);
				this.session.put("libraryAdmin", libraryAdmin);
				return "LIBRARYADMIN_SUCCESS";
			}else{
				actionErrorMessage="用户名或密码错误";
				return "INPUT";
			}

			
		}else if(loginRole.equalsIgnoreCase("systemAdmin")){
			systemAdmin=new SystemAdmin();
			systemAdmin.setSystemAdminID(username);
			systemAdmin.setSystemAdminPassword(password);
			if(facadeService.getSystemAdminService().isLogin(systemAdmin)){
				systemAdmin=facadeService.getSystemAdminService().getSystemAdminByID(username);
				this.session.put("systemAdmin", systemAdmin);
				return "SYSTEMADMIN_SUCCESS";
			}else{
				actionErrorMessage="用户名或密码错误";
				return "INPUT";
			}

		}
		
		return "INPUT";
	}

	
	
	public FacadeService getFacadeService() {
		return facadeService;
	}

	public void setFacadeService(FacadeService facadeService) {
		this.facadeService = facadeService;
	}

	public Reader getReader() {
		return reader;
	}

	public void setReader(Reader reader) {
		this.reader = reader;
	}

	public LibraryAdmin getLibraryAdmin() {
		return libraryAdmin;
	}

	public void setLibraryAdmin(LibraryAdmin libraryAdmin) {
		this.libraryAdmin = libraryAdmin;
	}

	public SystemAdmin getSystemAdmin() {
		return systemAdmin;
	}

	public void setSystemAdmin(SystemAdmin systemAdmin) {
		this.systemAdmin = systemAdmin;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getLoginRole() {
		return loginRole;
	}

	public void setLoginRole(String loginRole) {
		this.loginRole = loginRole;
	}




}
